package com.example.springbootdynamicdatasource.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootdynamicdatasource.entity.LightUser;
import com.example.springbootdynamicdatasource.entity.User;
import com.example.springbootdynamicdatasource.service.LightUserService;
import com.example.springbootdynamicdatasource.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2022-01-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LightUserService lightUserService;

    @GetMapping("/testdb1")
    public String testdb1() {
        List<User> userList = userService.getUserList();
        return JSON.toJSONString(userList);
    }

    @GetMapping("/testdb2Byname")
    public String testdb2Byname(@RequestBody String jsonName) {
        System.out.println(jsonName);
        JSONObject jsonObject = JSONObject.parseObject(jsonName);
        String userName = jsonObject.getString("userName");
        LightUser user = lightUserService.getUserByName(userName);
        return JSON.toJSONString(user);
    }

    @GetMapping(value="/testdb2",produces = "application/json; charset=utf-8")
    public String testdb2() {
        List<LightUser> userList = lightUserService.getUserList();
        return JSON.toJSONString(userList);
    }

}
